package com.bbcare.department.plat.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bbcare.comm.AppException;
import com.bbcare.comm.Constants;
import com.bbcare.comm.ErrorCode;
import com.bbcare.comm.ResultModel;
import com.bbcare.comm.ToolUtils;
import com.bbcare.department.plat.entity.Register;
import com.bbcare.department.plat.service.IRegisterService;

@Controller
@RequestMapping("/service/department")
public class RegisterController {
	Logger logger = Logger.getLogger(RegisterController.class);
	@Autowired
	private IRegisterService registerService;

	/*@ResponseBody
	@RequestMapping("/register/detail")
	public ResultModel<Register> getRegisterDetail(@RequestBody String jsonStr) throws Exception{
		logger.info("in getRegisterDetail----arg==" + jsonStr);
        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        String id = jsonEsb.getString("id");
        if(StringUtils.isEmpty(id)){
        	logger.debug("out getDepartmentBasic----arg== null");
        	return new ResultModel<Register>(null);
        }
        Register register = registerService.getRegisterById(id);
		logger.info("out getRegisterDetail----arg==");
		return new ResultModel<Register>(register);
	}*/

	@ResponseBody
	@RequestMapping("/register/detail")
	public ResultModel<List<Register>> getRegisterDetail(HttpServletRequest request,@RequestBody String jsonStr) throws Exception{
		logger.info("in getRegisterDetail----arg==" + jsonStr);
        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); //统一节点处理
		if(null == requestParam ){
			throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
		}

		// 创建人，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
//        String tenantId = (String) sessionMap.get("tenantId");// 租户id
        String deptId = (String) sessionMap.get("rootGroupId");

        List<Register> registers= registerService.getRegistersByDeptId(deptId);
		logger.info("out getRegisterDetail----arg==");
		return new ResultModel<List<Register>>(registers);
	}

	@ResponseBody
	@RequestMapping("/register/modify")
	public ResultModel<Map<String,String>> modifyRegister(HttpServletRequest request,@RequestBody String jsonStr) throws Exception{
		logger.info("in registerModify----arg==" + jsonStr);
        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); //统一节点处理
		if(null == requestParam ){
			throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
		}

        String registers= requestParam.getString("registers");
        if(null == registers ){
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参校验失败，挂号信息不能为空");
		}
        List<Register> list = JSON.parseArray(registers, Register.class);

     // 创建人，从session中获取
        Map<String, Object> sessionMap = ToolUtils.getSessionInfo(request);
//        String tenantId = (String) sessionMap.get("tenantId");// 租户id
        String createAuthor = (String) sessionMap.get("createAuthor");// 租户id
        String deptId = (String) sessionMap.get("rootGroupId");

        for(int i = 0 ;i<list.size();i++){
        		list.get(i).setDeptId(deptId);
			if(StringUtils.isEmpty(list.get(i).getId())){
				list.get(i).setCreateAuthor(createAuthor);
			}else{
				list.get(i).setUpdateAuthor(createAuthor);
			}
		}

        registerService.registerMoidify(list);
		logger.info("out modifyRegister----arg==");
//		Map<String,String> rspBody = new HashMap<String,String>();
//		rspBody.put("deptId",deptId);
		return new ResultModel<Map<String,String>>(null);
	}

	@ResponseBody
	@RequestMapping("/register/remove")
	public ResultModel<Register> deleteRegister(@RequestBody String jsonStr) throws Exception{
		logger.info("in deleteContact----arg==" + jsonStr);
        JSONObject jsonEsb = JSON.parseObject(jsonStr);
        JSONObject requestParam = jsonEsb.getJSONObject(Constants.INF_INPUTPARAM_COMMONFIELD); //统一节点处理
		if(null == requestParam ){
			throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR, "入参节点错误，无data节点！");
		}
        String ids= requestParam.getString("registerIds");

        if(StringUtils.isEmpty(ids)){
        	//返回空 或者返回校验失败
        	throw new AppException(ErrorCode.FOLLOWUP_EXCEPTION_TYPE_INPUT_DATA_VERIFY_ERROR,"入参校验失败，要删除挂号ID不能为空");
        }

        List<String> list = JSON.parseArray(ids, String.class);
        /*if(CollectionUtils.isEmpty(list)){
        	logger.debug("out deleteContact----arg== null");
        	return new ResultModel<DepartmentBasic>(null);
        }	*/
        registerService.deleteRegisterByIds(list);
		logger.info("out registerDelete----arg==");
		return new ResultModel<Register>(null);
	}
}
